package com.stylefeng.guns.modular.apple_app_manage.controller;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.shiro.ShiroKit;
import com.stylefeng.guns.modular.apple_app_manage.entity.ApplicationDTO;
import io.swagger.models.auth.In;
import org.apache.catalina.servlet4preview.http.HttpServletRequest;
import org.apache.tomcat.util.http.fileupload.servlet.ServletFileUpload;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.ui.Model;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import com.stylefeng.guns.modular.apple_app_manage.model.Application;
import com.stylefeng.guns.modular.apple_app_manage.service.IApplicationService;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletInputStream;
import java.io.IOException;

/**
 * 应用管理控制器
 *
 * @author fengshuonan
 * @Date 2020-06-20 20:31:36
 */
@Controller
@RequestMapping("/application")
public class ApplicationController extends BaseController {

    private String PREFIX = "/apple_app_manage/application/";

    @Autowired
    private IApplicationService applicationService;

    /**
     * 跳转到应用管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "application.html";
    }

    /**
     * 跳转到添加应用管理
     */
    @RequestMapping("/application_add")
    public String applicationAdd() {
        return PREFIX + "application_add.html";
    }

    /**
     * 跳转到修改应用管理
     */
    @RequestMapping("/application_update/{applicationId}")
    public String applicationUpdate(@PathVariable Integer applicationId, Model model) {
        Application application = applicationService.selectById(applicationId);
        model.addAttribute("item",application);
        LogObjectHolder.me().set(application);
        return PREFIX + "application_update.html";
    }

    /**
     * 获取应用管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(String condition) {
        EntityWrapper<Application> wrapper = new EntityWrapper<>();
        if(!ShiroKit.getUser().getRoleList().contains(1)) {
            wrapper.eq("user_id", ShiroKit.getUser().getId());
        }
        wrapper.last(" order by status desc, username");
        return applicationService.selectList(wrapper);
    }


    /**
     *  更新应用
     * */
    @RequestMapping("/update/{appId}")
    @ResponseBody
    public Object update(@RequestParam("ipaFile") MultipartFile file, @PathVariable("appId") Integer appId) {
        applicationService.updateApplicationById(appId, file);
        return SUCCESS_TIP;
    }

    /**
     * 新增应用管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(ApplicationDTO application) {
        applicationService.addNewApplication(application);
        return SUCCESS_TIP;
    }

    /**
     * 删除应用管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer applicationId) {
        applicationService.deleteApplicationById(applicationId);
        return SUCCESS_TIP;
    }

    /**
     *  上下架应用
     * */
    @RequestMapping(value = "/onoff")
    @ResponseBody
    public Object onOff(@RequestParam Integer applicationId) {
        applicationService.onOffApplicationById(applicationId);
        return SUCCESS_TIP;
    }

    /**
     * 应用管理详情
     */
    @RequestMapping(value = "/detail/{applicationId}")
    @ResponseBody
    public Object detail(@PathVariable("applicationId") Integer applicationId) {
        return applicationService.selectById(applicationId);
    }

    /**
     * 应用管理详情
     */
    @RequestMapping(value = "/appNameList")
    @ResponseBody
    public Object getAppNames() {
        return applicationService.getAppNameList();
    }
}
